/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { HvigorBuildConst } from '../../../../common/options/common-const.js';
import { Project } from '../../../../external/core/hvigor-core-node.js';
import { HvigorLogger } from '../../../../log/hvigor-log.js';

import { AbstractExecutePipeline } from './abstract-execute-pipeline.js';
import { ExecuteModulePipeline } from './execute-module-pipeline.js';
import { ExecuteProjectPipeline } from './execute-project-pipeline.js';
import { ExecuteUndefinedPipeline } from './execute-undefined-pipeline.js';

const _log = HvigorLogger.getLogger('hvigor-execute-mode-factory');

/**
 * CoreTask 执行配置
 *
 * @since 2022/5/18
 */
export interface TaskExecuteOptions {
  toRunTasks: string[];
  isNeedSync: boolean;
}

/**
 * 一个简单工厂，用于根据命令行中用户指定的模式，创建出不同的pipeline管道
 *
 * @since 2022/6/18
 */
export class ExecuteModeFactory {
  private readonly project: Project;
  private readonly taskOptions: TaskExecuteOptions;

  constructor(project: Project, taskOptions: TaskExecuteOptions) {
    this.project = project;
    this.taskOptions = taskOptions;
  }

  /**
   * 根据用户指定的不同模式创建不同的执行管道
   * -m project ->ExecuteProjectPipeline
   * -m module ->ExecuteModulePipeline
   * 不指定 ->ExecuteUndefinedPipeline
   *
   * @param {string} commandMode
   * @returns {AbstractExecutePipeline}
   */
  public getExecutePipeline(commandMode: string): AbstractExecutePipeline {
    switch (commandMode) {
      case HvigorBuildConst.PROJECT_MODE:
        return new ExecuteProjectPipeline(this.project, this.taskOptions, true);
      case HvigorBuildConst.MODULE_MODE:
        return new ExecuteModulePipeline(this.project, this.taskOptions);
      case undefined:
        return new ExecuteUndefinedPipeline(this.project, this.taskOptions);
      default:
        _log.error(`Unknown mode '${commandMode}' is specified in the command line. Please check.`);

        throw new Error('Command status error.');
    }
  }
}
